
// plugin name : $.include  异步加载
// $.include('file/ajaxa.js');$.include('file/ajaxa.css');
// or $.includePath  = 'file/';$.include(['ajaxa.js','ajaxa.css']);

$.extend({
     includePath: '',
     include: function(file) {
        var files = typeof file == "string" ? [file]:file;
        for (var i = 0; i < files.length; i++) {
            var name = files[i].replace(/^\s|\s$/g, "");
            var att = name.split('.');
            var ext = att[att.length - 1].toLowerCase();
            var isCSS = ext == "css";
            var tag = isCSS ? "link" : "script";
            var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
            var link = (isCSS ? "href" : "src") + "='" + $.includePath + name + "'";
            if ($(tag + "[" + link + "]").length == 0) $('body').append("<" + tag + attr + link + "></" + tag + ">");
        }
   }
});

$(function(){

    // $('#markdown').addClass('animated bounceInLeft');
    // $('#scrollbar').addClass('animated bounceInRight');
    // $('#keyboard').addClass('animated bounceInRight');
    // $('#path').addClass('animated bounceInLeft');

    $("p img").each(function() {
        var strA = "<a class='fancyimg' href='" + this.src + "' title='"+this.title+"'></a>";
        $(this).wrapAll(strA);
    });

      $('.fancyimg').fancybox({
        type:'image',
        openEffect  : 'elastic',
        closeEffect : 'elastic',
        helpers : {
            title : {
                type : 'float'
            }
        }
    });
      
    // sup 标记中a的titiel添加
    $('.footnotes li[id*="fn:"]').addClass('alert alert-info');
    $('.markdown img').addClass('img-polaroid');

    $('a[href*="#fn:"]').each(function(){
        str = $('.footnotes li[id="fn:'+$(this).text()+'"]').text();
        str = str.replace(/\n/g, ' ');
        str = str.replace(/&#160;/g, ' ');
        str = str.replace(/&#8617;/g, ' ');
        $(this).attr('title',str);
    });

    // 为 a 添加 tooltip提示
    //$('sup a[href*="#fn:"]').tooltip();
    $('a[title]').tooltip({placement:'right'});

    $('a[href]').each(function(){
        a = $(this);
        a.attr('href',a.attr('href').replace('.md',''))
    });

    //外链处理
    $(document.links).filter(function() {return this.hostname != window.location.hostname;}).attr('target', '_blank');

    $('a[href=#comments]').click(function(){
        body_scroll($('#comments').offset().top);
    })

    // 背景色改变
    $('#markdown-color .btn').hover(
            function(){$(this).animate({"height":"40px"},100)},
            function(){$(this).animate({"height":"24px"},100)}
    );
    $('#markdown-color .btn').click(function(){
        $('#markdown').css({'background-color':$(this).attr('bg'),'color':$(this).attr('fg')})
    });

	if (!m) {
    // 初始窗口
    if ($(window).width()< 740 ) {
        $('.container').css('width','98%');
        $('#markdown').css({'width':'100%','display':'block'});
        $('#comments').css({'width':'100%'});
        $('#sides').css({'width':'100%'});
        $('#nav').css('width','220px');
        $('#nav').css({'left':$('#markdown').width()+$('#markdown').offset().left-240 });
        $('#nav').removeClass('right');
        $('#nav').addClass('left');
    }else{
        ($(window).width() > 1090)? $('.container').css('width','1090px'):$('.container').css('width','98%');
        $('#nav').css('width',$('#keyboard').width());
        $('#nav').css({'left':$('#markdown').width()+$('#markdown').offset().left+3});
    }
    $('#scrollbar').css({'left':$('#markdown').width()+$('#markdown').offset().left-20 });
	}

    // 判定窗口大小来隐藏标题列表
    $(window).resize(function(){
        var winHeight = $(window).height();
        var winWidth = $(window).width();

        (winWidth > 1090)? $('.container').css('width','1090px'):$('.container').css('width','98%');

        if ( winWidth > 740 ) {
            $('#markdown').css({'width':'75%'});
            $('#comments').css({'width':'75%'});
            $('#sides').css({'width':'24%'});
            $('#nav').css('width',$('#keyboard').width());
            $('#nav').css({'left':$('#markdown').width()+$('#markdown').offset().left+5});
            if(!$('#nav').hasClass('right')){
                $('#nav').removeClass('left');
                $('#nav').addClass('right');
            }
        }else{
            $('#markdown').css({'width':'100%'});
            $('#comments').css({'width':'100%'});
            $('#sides').css({'width':'100%'});
            $('#nav').css('width','220px');
            $('#nav').css('left',$('#scrollbar').offset().left-225);
            if(!$('#nav').hasClass('left')){
                $('#nav').removeClass('right');
                $('#nav').addClass('left');}
        }
        $('#scrollbar').css({'left':$('#markdown').width()+$('#markdown').offset().left-20 });
    });

	hljs.tabReplace = '    ';
	hljs.initHighlightingOnLoad();
	//$.include('https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js');
});
